package com.lizx.interceptor;

import com.lizx.staff.service.EmployeeService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private EmployeeService employeeService;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(createYourInterceptor(employeeService))
                .addPathPatterns("/**") // 全部拦截
                .excludePathPatterns("/employee/add");  // 放行一些地址
    }

    /**
     * 实现业务的过滤器里面，如果不加这块，employeeService永远为null，这块就是解决employeeService为null问题的
     * @param employeeService
     * @return
     */
    @Bean
    public HandlerInterceptor createYourInterceptor(EmployeeService employeeService) {
        return new MyInterceptor(employeeService);
    }

}
